#include "curl.h"
#include <stdio.h>
#include "cJSON.h"
size_t writeCallback(void *contents,size_t size,size_t nmemb,void *userp)
{
    size_t realsize = size *nmemb;
    //存放获取到的数据，比如存储到文件或其他的一些操作
    char *result = (char*)contents;
    printf("%s\n",result);
    cJSON *jsonstr = cJSON_Parse(result);
    if (jsonstr != NULL)
    {
        cJSON *successRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"success");
        cJSON *codeRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"code");
        cJSON *messageRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"message");
        if (cJSON_IsTrue(successRoot))
        {
            printf("%s\n",messageRoot->valuestring);
        }else
        {
            perror("Network exception\n");
        }
    }
    return realsize;
}
int main(int argc, char const *argv[])
{
    //初始化curl
    CURL *curl = curl_easy_init();
    //给curl设置对应选项(目标地址、回调函数、请求参数、发送文件)
    curl_easy_setopt(curl,CURLOPT_URL,"http://47.109.65.208/config.json");
    //发送请求参数
    //curl_easy_setopt(curl,CURLOPT_POSTFIELDS,parmStr);
    //设置回调函数
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeCallback);
    //发送请求
    CURLcode code = curl_easy_perform(curl);
    if (code != CURLE_OK)
    {
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(code));
    }
    //清理
    curl_easy_cleanup(curl);
    return 0;
}
